﻿using DapperExtensions;
using KD.Service.HR.Utility;
using Logistics.Model;
using Logistics.Model.QueryModel;
using Logistics.Service;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace Logistics.WebApi.Controllers
{
    /// <summary>
    /// 签约申请
    /// </summary>
      [MyAuthority]
    public class CompanyApplyController:ApiController
    {
        private CompanyApplyService service = new CompanyApplyService();
        private ContractService contractService = new ContractService();
        private WuLiuCompanyService wuliuinfo = new WuLiuCompanyService();
          /// <summary>
          /// 获取物流签约企业的数据
          /// </summary>
          /// <param name="query"></param>
          /// <returns></returns>
        [HttpPost]
        public ResponseModel GetList(CompanyQuery query)
        {
            var responseModel = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var companyService = new CompanyService();
                var company = companyService.GetCompanyByAccountId(accountId);
                var list = new List<IPredicate>();
                if (!string.IsNullOrEmpty(query.companyname))
                    list.Add(Predicates.Field<wuliu_wlcompany_business>(f => f.wuliu_name, Operator.Like, "%" + query.companyname + "%"));
                list.Add(Predicates.Field<wuliu_wlcompany_business>(f => f.state, Operator.Eq, 1));
                if (!string.IsNullOrEmpty(query.cityid))
                    list.Add(Predicates.Field<wuliu_wlcompany_business>(f => f.city_id, Operator.Like, "%" + query.cityid + "%"));
                var pg = Predicates.Group(GroupOperator.And, list.ToArray());
                responseModel.mod = service.GetList(pg, query.page - 1, query.pagesize, company.company_id);
              responseModel.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }

        [HttpPost]
        /// <summary>
        /// 企业申请向物流提出申请，需要往物流签约表插
        /// </summary>
        /// <param name="apply"></param>
        /// <returns></returns>
        public ResponseModel Apply(wuliu_wlcompany_apply_query apply)
        {
            var resultmod = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var companyService = new CompanyService();
                var company = companyService.GetCompanyByAccountId(accountId);
                var wuliu = wuliuinfo.Get((int)apply.wuliu_id);
                apply.wuliu_name = wuliu.wuliu_name;
                apply.wuliu_id = wuliu.wuliu_id;
                apply.wuliu_no = wuliu.wuliu_no;
                apply.wuliu_city_name = wuliu.city_name;
                apply.wuliu_city_id = wuliu.city_id;
                apply.Operator = accountId.ToString();
                apply.company_id = company.company_id;
                apply.company_name = company.company_name;
                apply.company_no = company.company_no;
                if(company.city_id_x!=null)
                {
                    var cityid = company.city_id_x.Split('|');
                    if (cityid.Count() > 0)
                    {
                        if (cityid.Length == 1)
                        {
                            if (cityid[0] != null)
                                apply.company_city_id3 = Convert.ToInt32(cityid[0]);
                        }
                        if (cityid.Length == 2)
                        {
                            if (cityid[0] != null)
                                apply.company_city_id1 = Convert.ToInt32(cityid[0]);
                            if (cityid[1] != null && cityid.Length > 1)
                                apply.company_city_id2 = Convert.ToInt32(cityid[1]);
                        }
                        if (cityid.Length == 3)
                        {
                            if (cityid[0] != null)
                                apply.company_city_id1 = Convert.ToInt32(cityid[0]);
                            if (cityid[1] != null)
                                apply.company_city_id2 = Convert.ToInt32(cityid[1]);
                            if (cityid[2] != null)
                                apply.company_city_id3 = Convert.ToInt32(cityid[2]);
                        }
                    }
                }
                if(wuliu.city_id_x!=null)
                {
                    if(!string.IsNullOrEmpty(wuliu.city_id_x))
                    {
                        var wuliucityid = wuliu.city_id_x.Split('|');
                        if (wuliucityid.Count() > 0)
                        {
                            if (wuliucityid.Length == 1)
                            {
                                if (wuliucityid[0] != null)
                                    apply.wuliu_city_id3 = Convert.ToInt32(wuliucityid[0]);
                            }
                            if (wuliucityid.Length == 2)
                            {
                                if (wuliucityid[0] != null)
                                    apply.wuliu_city_id1 = Convert.ToInt32(wuliucityid[0]);
                                if (wuliucityid[1] != null)
                                    apply.wuliu_city_id2 = Convert.ToInt32(wuliucityid[1]);
                            }
                            if (wuliucityid.Length == 3)
                            {
                                if (wuliucityid[0] != null)
                                    apply.wuliu_city_id1 = Convert.ToInt32(wuliucityid[0]);
                                if (wuliucityid[1] != null)
                                    apply.wuliu_city_id2 = Convert.ToInt32(wuliucityid[1]);
                                if (wuliucityid[2] != null)
                                    apply.wuliu_city_id3 = Convert.ToInt32(wuliucityid[2]);
                            }
                        }
                    }
                }
                apply.apply_month = Convert.ToInt32(DateTime.Now.ToString("yyyyMM"));
                apply.apply_time = DateTime.Now;
                apply.apply_flag = "1";
                WLApplyService aService = new WLApplyService();
                aService.Add(apply);
                resultmod.InitResultState(StateEnum.Success);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }
      
    }
}